package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.CouponCodeDTO;
import com.quanquan.management.dto.PayNotifyDTO;
import com.quanquan.management.request.CouponRecordRequestBody;
import com.quanquan.management.request.PayNotifyRequestBody;
import entity.ExcelData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.ExcelUtil;
import utils.Request;
import utils.StringUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;

/**
 * 功能描述:
 *
 * @ClassName: ParkingService
 * @Author: xiaoqianh丶
 * @Date: 2019-04-14 19:23
 * @Version: V1.0
 */
@Service
public class ParkingService {
    @Value("${module-url.storeUrl}")
    private String storeUrl;

    @Value("${module-url.parkingUrl}")
    private String parkingUrl;

    public PageDTO<CouponCodeDTO> listAllParkings(CouponRecordRequestBody requestBody) {
        String json = Request.postJson(storeUrl + "coupon/parking_record", requestBody);
        PageDTO<CouponCodeDTO> pageDTO = new PageDTO<>();
        if (StringUtil.isEmpty(json)) {
            return pageDTO;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<CouponCodeDTO> userDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                CouponCodeDTO object = list.getObject(i, CouponCodeDTO.class);
                userDTOS.add(object);
            }
            data.setList(userDTOS);
            return data;
        }
        return pageDTO;
    }
    public void export(HttpServletResponse response, CouponRecordRequestBody requestBody) throws Exception {
        List<String> titles = new LinkedList<>();
        titles.add("会员车牌号");
        titles.add("会员名称");
        titles.add("手机号");
        titles.add("会员卡号");
        titles.add("优惠券名称");
        titles.add("优惠券券码");
        titles.add("优惠券领取时间");
        titles.add("优惠券使用时间");
        titles.add("优惠券有效时间");
        titles.add("优惠券过期时间");
        ExcelData data = ExcelUtil.getExcelData();
        data.setTitles(titles);
        PageDTO<CouponCodeDTO> pageDTO = this.listAllParkings(requestBody);
        List<CouponCodeDTO> list = pageDTO.getList();
        List<List<Object>> rows = new LinkedList<>();
        list.forEach(vo -> {
            List<Object> row = new LinkedList<>();
            row.add(vo.getCarNumber());
            row.add(vo.getUseName());
            row.add(vo.getPhone());
            row.add(vo.getUseMemberCard());
            row.add(vo.getCouponName());
            row.add(vo.getCouponCode());
            row.add(vo.getAddTime());
            row.add(vo.getUseTime());
            row.add(vo.getUseTimeStart());
            row.add(vo.getUseTimeEnd());
            rows.add(row);
        });
        data.setRows(rows);
        String now = DateUtils.currentDate(DateUtils.DATE_HOUR_SECOND);
        ExcelUtil.exportExcel(response,"停车券使用记录" + now,data);
    }


    /**
     * 停车缴费记录
     *
     * @param requestBody
     * @return
     */
    public PageDTO<PayNotifyDTO> parkingRecord(PayNotifyRequestBody requestBody) {
        String json = Request.postJson(parkingUrl + "parking/parking_record", requestBody);
        PageDTO<PayNotifyDTO> pageDTO = new PageDTO<>();
        if (StringUtil.isEmpty(json)) {
            return pageDTO;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<PayNotifyDTO> payNotifyDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                PayNotifyDTO object = list.getObject(i, PayNotifyDTO.class);
                payNotifyDTOS.add(object);
            }
            data.setList(payNotifyDTOS);
            return data;
        }
        return pageDTO;
    }


    public void exportParkingRecord(HttpServletResponse response, PayNotifyRequestBody requestBody) throws Exception {
        List<String> titles = new LinkedList<>();
        titles.add("车牌号");
        titles.add("订单号");
        titles.add("券费用");
        titles.add("现金费用");
        titles.add("总费用");
        titles.add("缴费时间");
        ExcelData data = ExcelUtil.getExcelData();
        data.setTitles(titles);
        PageDTO<PayNotifyDTO> pageDTO = this.parkingRecord(requestBody);
        List<PayNotifyDTO> list = pageDTO.getList();
        List<List<Object>> rows = new LinkedList<>();
        list.forEach(vo -> {
            List<Object> row = new LinkedList<>();
            row.add(vo.getPlateNumber());
            row.add(vo.getOrderId());
            row.add(vo.getCoupon());
            row.add(vo.getFee());
            row.add(vo.getCoupon().add(vo.getFee()));
            row.add(vo.getTime());
            rows.add(row);
        });
        data.setRows(rows);
        String now = DateUtils.currentDate(DateUtils.DATE_HOUR_SECOND);
        ExcelUtil.exportExcel(response,"停车缴费记录" + now,data);
    }


}
